package cloud.alibaba.study.entity.base;

import java.io.Serializable;
import java.text.MessageFormat;

import org.apache.commons.lang3.StringUtils;

import cloud.alibaba.study.utils.GsonUtil;
import lombok.Data;

@Data
public class ResponseVo implements Serializable {

	private static final long serialVersionUID = -3987058891400255687L;

	private Boolean isOk = false;
	private String respCode;
	private String respMsg;
	private Object respData;

	public ResponseVo() {
	}

	public ResponseVo(RespCodeEnum repCodeEnum) {
		this.setRespCodeEnum(repCodeEnum);
	}

	// 成功
	public static ResponseVo success() {
		return ResponseVo.success(RespCodeEnum.SUCCESS.getDesc());
	}

	// 自定义成功消息
	public static ResponseVo success(String message) {
		ResponseVo responseModel = new ResponseVo();
		responseModel.setIsOk(true);
		responseModel.setRespCode(RespCodeEnum.SUCCESS.getCode());
		responseModel.setRespMsg(message);
		return responseModel;
	}

	// 自定义成功数据
	public static ResponseVo success(Object data) {
		ResponseVo responseModel = new ResponseVo();
		responseModel.setIsOk(true);
		responseModel.setRespCode(RespCodeEnum.SUCCESS.getCode());
		responseModel.setRespMsg(RespCodeEnum.SUCCESS.getDesc());
		responseModel.setRespData(data);
		return responseModel;
	}

	// 自定义成功数据
	public static ResponseVo success(Object data, String message) {
		ResponseVo responseModel = new ResponseVo();
		responseModel.setIsOk(true);
		responseModel.setRespCode(RespCodeEnum.SUCCESS.getCode());
		responseModel.setRespMsg(message);
		responseModel.setRespData(data);
		return responseModel;
	}

	public static ResponseVo failure() {
		return failure(RespCodeEnum.FAIL.getDesc());
	}

	// 自定义失败消息
	public static ResponseVo failure(String message) {
		ResponseVo responseModel = new ResponseVo();
		responseModel.setRespCode(RespCodeEnum.FAIL.getCode());
		responseModel.setRespMsg(message);
		return responseModel;
	}

	// 失败
	public static ResponseVo failure(RespCodeEnum respEnum) {
		ResponseVo responseModel = new ResponseVo();
		responseModel.setRespCodeEnum(respEnum);
		return responseModel;
	}

	/**
	 * 自定义处理错误消息
	 * 
	 * @param code    错误码
	 * @param message 错误消息
	 * @return
	 */
	public static ResponseVo failure(String code, String message) {
		if (StringUtils.isBlank(code)) {
			return failure(message);
		}
		ResponseVo responseModel = new ResponseVo();
		responseModel.setRespCode(code);
		responseModel.setRespMsg(message);
		return responseModel;
	}

	// 自定义code和失败信息
	public static ResponseVo failure(RespCodeEnum respCodeEnum, String message) {
		ResponseVo responseModel = new ResponseVo();
		responseModel.setRespCode(respCodeEnum.getCode());
		responseModel.setRespMsg(message);
		return responseModel;
	}

	// 异常信息统一处理
	public static ResponseVo errorExceptionMsg(String message) {
		ResponseVo responseModel = new ResponseVo();
		responseModel.setRespCode(RespCodeEnum.EXCEPTION.getCode());
		responseModel.setRespMsg(RespCodeEnum.EXCEPTION.getDesc() + ": " + message);
		return responseModel;
	}

//	    @Override
//	    public String toString() {
//	        return ToStringBuilder.reflectionToString(this);
//	    }

	public String toJson() {
		// return JSONObject.toJSONString(this);
		return GsonUtil.toJsonDateFormat(this);
	}

	//对象转换
	public <T> T getRespDate(Object obj, Class<T> targetClazz) {
		T t = GsonUtil.fromJson(GsonUtil.toJson(obj), targetClazz, GsonUtil.DATE_FORMAT);
		return t;
	}

//	public boolean isError() {
//		return !isSuccess();
//	}
//
//	public boolean isSuccess() {
//		if (this == null) {
//			return false;
//		}
//		return StringUtils.equals(this.RespCode, RespCodeEnum.SUCCESS.getCode());
//	}

	public void setRespCodeEnum(RespCodeEnum repCodeEnum) {
		this.respCode = repCodeEnum.getCode();
		this.respMsg = repCodeEnum.getDesc();
	}

	public void setRespCodeEnum(RespCodeEnum respCodeEnum, Object... args) {
		this.respCode = respCodeEnum.getCode();
		this.respMsg = MessageFormat.format(respCodeEnum.getDesc(), args);
	}

}
